package com.yinhai.message.web.bpo;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import com.yinhai.core.common.api.base.IPage;
import com.yinhai.core.common.api.util.ValidateUtil;
import com.yinhai.core.service.ta3.domain.service.TaBaseService;
import com.yinhai.message.api.bean.TMUser;
import com.yinhai.message.api.vo.TMMessageVo;
import com.yinhai.message.web.dao.IUserContactDao;
import com.yinhai.message.web.po.UserContactPo;
import com.yinhai.message.web.vo.MessageWebQueryVo;
import com.yinhai.message.web.vo.MessageWebUserVo;
import com.yinhai.modules.org.api.util.IOrgConstants;
import com.yinhai.modules.org.ta3.domain.dao.IOrgDao;
import com.yinhai.modules.org.ta3.domain.po.impl.OrgPo;

/**
 * Created by xiepx on 2017/3/28.
 */
public class MessageWebBpoImpl extends TaBaseService implements IMessageWebBpo {
    @Resource(name = "orgDao")
    private IOrgDao orgDao;
    @Resource(name = "userContactDao")
    private IUserContactDao userContactDao;

    @Override
    public IPage<MessageWebUserVo> queryMessageUser(MessageWebQueryVo msgWebVo) {
        String userClassName = "com.yinhai.modules.org.ta3.domain.po.impl.UserPo";
        String userContactClassName = "com.yinhai.message.web.po.UserContactPo";
        String positionClassName = "com.yinhai.modules.org.ta3.domain.po.impl.PositionPo";
        String userPositionClassName = "com.yinhai.modules.org.ta3.domain.po.impl.UserPositionPo";
        String orgClassName = "com.yinhai.modules.org.ta3.domain.po.impl.OrgPo";
        //hql
        StringBuilder hql = new StringBuilder();
        hql.append("select distinct new com.yinhai.message.web.vo.MessageWebUserVo(" + "u.userid ,u.loginid,u.name,uc.tel,uc.email,uc.wechat,uc.phoneapp,uc.imid) ")
                .append("from "+ userClassName + " u,"+ userContactClassName + " uc,"+orgClassName+" o,"+positionClassName+" p,"+userPositionClassName+" up ")
                .append(" where u.userid = uc.userid and u.userid = up.id.userid and up.id.positionid = p.positionid and p.orgid = o.orgid ")
//                .append(" and p.positiontype=").append(IOrgConstants.POSITION_TYPE_PERSON)
                .append(" and (u.destory is null or u.destory=").append(IOrgConstants.COMMON_DESTORY_NO).append(") ");

        //参数
        Map<String, Object> params = new HashMap<String, Object>();

        //模糊查询姓名
        if (ValidateUtil.isNotEmpty(msgWebVo.getName())) {
            hql.append(" and u.name like :name ");
            params.put("name","%"+msgWebVo.getName()+"%");
        }

        // 有效标志
        if (ValidateUtil.isNotEmpty(msgWebVo.getEffective())) {
            hql.append(" and p.effective = :effective ");
            params.put("effective",msgWebVo.getEffective());
        }

        Long orgid = msgWebVo.getOrgId();
        OrgPo o = orgDao.getDepart(orgid);
        if (o != null) {
            if (IOrgConstants.IS_CHILDRENORG.equals(msgWebVo.getIsIncludeSun())) {
                hql.append(" and p.orgidpath like '").append(o.getOrgidpath()).append("%' ");
            } else {
                hql.append(" and p.orgid =:orgid ");
                params.put("orgid",msgWebVo.getOrgId());
            }
        }

        return userContactDao.queryMessageUsers(hql.toString(),params,msgWebVo.getStart(),msgWebVo.getLimit(),msgWebVo.getGridId());

    }

    @Override
    public List<TMUser> buildTMUserListByVo(TMMessageVo vo) {
        List<TMUser> newList = new ArrayList<>();
        List<TMUser> userList = vo.getUserlist();
        List<String> messageTypes = vo.getMessage().getMessageTypes();
        if (messageTypes.size() == 1 && "0".equals(messageTypes.get(0))) {
            //若只用dwr发，则不需进数据库查
            return userList;
        }
        for (TMUser user : userList) {
            String hql = "select new com.yinhai.message.web.po.UserContactPo(up.userid,up.tel,up.email,up.wechat,up.phoneapp,up.imid) from com.yinhai.message.web.po.UserContactPo up where up.userid = ?";
            UserContactPo po = userContactDao.queryContactByUserId(hql, Long.parseLong(user.getUserId()));
            if (po != null) {
                TMUser tmUser = po.toTMUser();
                tmUser.setAppId(user.getAppId());
                newList.add(tmUser);
            }
        }
        return newList;
    }

	@Override
	public void buildSendTMUser(TMUser user) {
		 String hql = "select new com.yinhai.message.web.po.UserContactPo(up.userid,up.tel,up.email,up.wechat,up.phoneapp,up.imid) from com.yinhai.message.web.po.UserContactPo up where up.userid = ?";
         UserContactPo po = userContactDao.queryContactByUserId(hql, Long.parseLong(user.getUserId()));
         if (po != null) {
        	 user.setImId(po.getImid());
         }
	}

}
